<?php

	/**
	 * File : getperm.php
	 * Description : Renvoi les droits d'un fichier en XML
	 * Date : 07.08.2012 14:14:07
	 * 
	 * This file is part of Phenix.  Phenix is free software: you can
	 * redistribute it and/or modify it under the terms of the GNU General Public
	 * License as published by the Free Software Foundation, version 2.
	 *
	 * This program is distributed in the hope that it will be useful, but WITHOUT
	 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
	 * FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
	 * details.
	 *
	 * You should have received a copy of the GNU General Public License along with
	 * this program; if not, write to the Free Software Foundation, Inc., 51
	 * Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
	 *
	 * Nils Schaetti <n.schaetti@gmail.com>
	**/

	// En tête XML
	echo "<?xml version=\"1.0\"?>";

	// Includes
	require_once('core/libphenix.php');
	require_once('KTK/ktk.php');

	// Buffering
	ob_start();
	
	// Document type XML
	header("content-type: application/xml");

	// Démarre la session
	session_start();
	
	// Connexion à la base de données
	ktkConnectDB();
	
	// Vérifie le login
	if(!ktkCheckLogin())
	{
		echo '<files><error>Session expired</error></files>';
		exit;
	}

	// Connexion ssh
	if(($ssh_handler = lp_ssh_connect('localhost',$_SESSION['username'],$_SESSION['password'])) < 0)
	{
		echo "Impossible de se connecter via SSH";
		exit;
	}
	
	// Répertoire à bouger
	if(!isset($_GET['file']))
		$file = '.';
	else
		$file = $_GET['file'];
		
	// Tag fichier
	echo '<perm>';
		
	// Execute une commande
	if(($file_desc = lp_ls($ssh_handler,$file,false,'')) === false)
	{
		// Erreur
		echo "<error>You can't get permissions of this file</error>";
	}
	else
	{
		
		// Permissions du fichier
		$perms = $file_desc[0]->getRights();
		
		// Permissions de l'utilisateur
		echo '<user>';
		echo $perms[1];
		echo $perms[2];
		echo $perms[3];
		echo '</user>';
		
		// Permissions du groupe
		echo '<group>';
		echo $perms[4];
		echo $perms[5];
		echo $perms[6];
		echo '</group>';
		
		// Permissions des autres
		echo '<others>';
		echo $perms[7];
		echo $perms[8];
		echo $perms[9];
		echo '</others>';
	}
	
	// Ferme le tag fichier
	echo '</perm>';
		
	// Ferme la connexion ssh
	lp_ssh_close($ssh_handler);	

	// Fin buffering
	ob_end_flush();

?>
